using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Castle.Core;
using DecisionMaking.Domain;
using DecisionMaking.BusinessLogic;

namespace DecisionMaking.WebApp.Controllers
{
	public class BaseController : Controller
	{
		/// <summary>
		/// Injected by Windsor 
		/// </summary>
		public IAccountManager AccountManager {protected get; set;}		
		
		protected Account CurrentAccount 
        {
			get 
			{ 
				var account = (Account)this.HttpContext.Session[typeof(Account).Name];

			    if (account == null && HttpContext.User.Identity.IsAuthenticated)
			    {
			        account = AccountManager.GetAccountByEmail(HttpContext.User.Identity.Name);
			        CurrentAccount = account;
			    }

			    return account;
			}

			set { this.HttpContext.Session[typeof(Account).Name] = value; }
		}

	    protected ActionResult NotFound(string message, params object[] args)
		{
			return new HttpNotFoundResult(string.Format(message, args));
		}

	    protected ActionResult NotFound(EntityNotFoundException e)
		{
			return NotFound(e.Message, e.Data["id"]);
		}

	    protected ActionResult AccessDenied(AccessDeniedException e)
		{
			return new HttpUnauthorizedResult();
		}

	    protected ActionResult AccessDenied()
		{
			return new HttpUnauthorizedResult();
		}

	    protected override void Initialize(RequestContext requestContext)
	    {
            base.Initialize(requestContext);
            ViewData["currentAccount"] = CurrentAccount;
	    }

	}
	
}

